﻿using ConfExpert.Forms.Base;

using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraReports.UI;
using DevExpress.XtraVerticalGrid.Events;
using DevExpress.XtraVerticalGrid.Rows;
using DevTeam;

using System;
using System.Drawing;
using System.Windows.Forms;

namespace ConfExpert.Forms.Depo.Aksesuar
{
    public partial class SatinalmaSiparisleri : XForm
    {
        public SatinalmaSiparisleri()
        {
            InitializeComponent();
        }

        private void SatinalmaTalepleri_Load(object sender, EventArgs e)
        {
            

            gvAksPOSip.Init("GetAksPOatVendor", 0, string.Empty, 0, 0, string.Empty, 0, 0, 0, string.Empty, WhoAmI);
            gvAksPOSip.AddDetailGrid(gvAksPOSipLines, "AksPOSipId");

            gvAksPOSipLines.AddColumn("Tutar", typeof(int), "QtyOrdered*AnlasilanFiyat");
            gvAksPOSip.AddColumn("ToplamAdet", typeof(decimal), "SUM(Child({FK}).QtyOrdered)");
            gvAksPOSip.AddColumn("ToplamTutar", typeof(decimal), "SUM(Child({FK}).Tutar)");

            gvAksPOSip.MakeColumnInvisible("DvzId");
            gvAksPOSipLines.MakeColumnInvisible("VendorId","AksPOLinesId","AksPOSipId","AksPOId","UserId");

            gvAksPOSip.SetCaptions("VendorId:Tedarikci", "SipNo:TalepNo","SipVerenId:Sipariş Veren");
            gvAksPOSipLines.SetCaptions("QtyNeeded:Talep Adedi", "QtyOrdered:Sipariş Adedi", "QtyGelen:Gelen Adet",  "Notes:Notlar"); 

            gvAksPOSip.MakeReadOnly();
            gvAksPOSipLines.MakeReadOnly();

            gvAksPOSip.FormatAsNumber(0, "ToplamAdet");
            gvAksPOSip.FormatAsNumber(1, "ToplamTutar");
            gvAksPOSipLines.FormatAsNumber(3, "AnlasilanFiyat");
            gvAksPOSipLines.FormatAsNumber(1, "Tutar");
            gvAksPOSipLines.FormatAsNumber(0, "QtyOrdered", "QtyNeeded","QtyGelen");

            gvAksPOSip.AddSummary("ToplamAdet", "ToplamTutar");
            gvAksPOSipLines.AddSummary("QtyOrdered", "Tutar");

            GridManager gm = GridManager.GetManager(gvAksPOSip);
            gm.GridMenu.AddMenu("Sipariş föyü çıkar", PrintPurchaseFoy);
            gm.GridMenu.AddMenu("Siparişi iptal et", CancelAksPOSip);

            gm = GridManager.GetManager(gvAksPOSipLines);
            gm.GridMenu.AddMenu("Sipariş satırını iptal et", CancelAksPOSipLines);

            btnGetir.Click += new EventHandler(btnGetir_Click);
            btnTemizle.Click += new EventHandler(btnTemizle_Click);

            CreateVerticalGrid();
        }

        private void CancelAksPOSipLines(object sender, EventArgs e)
        {
            if (gvAksPOSipLines.FocusedRowHandle > -1)
            {
                int AksPOLinesId = gvAksPOSipLines.GetFocusedRowCellValue("AksPOLinesId").ToInt();
                int GelenAdet = gvAksPOSipLines.GetFocusedRowCellValue("QtyGelen").ToInt();
                string Aksesuar = gvAksPOSipLines.GetFocusedRowCellDisplayText("AksesuarId").ToString();

                if (GelenAdet>0)
                {
                    ShowErrorMessage("Girişi yapılmış olan sipariş iptal edilemez");
                    return;
                }

                if (XtraMessageBox.Show("Aksesuar : " + Aksesuar
                                    + "\n\n Bu aksesuarı İPTAL ETMEK istediğinize emin misiniz?", "Kritik!", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No)
                    return;

                ExecuteNonQuery("DelAksPOSipLines", AksPOLinesId);
                Yenile();
            }
        }

        private void CancelAksPOSip(object sender, EventArgs e)
        {
            if (gvAksPOSip.FocusedRowHandle>-1)
            {
                int aksPOSipId = gvAksPOSip.GetFocusedRowCellValue("AksPOSipId").ToInt();
                ExecuteNonQuery("DelAksPOSip", aksPOSipId);
                Yenile(); 
            }
            
        }

        private void PrintPurchaseFoy(object sender, EventArgs e)
        {
            string ids = gvAksPOSip.GetSelectedValues("AksPOSipId");
            Rapor.Aksesuar.xrAksesuarSiparisFoy asf = new Rapor.Aksesuar.xrAksesuarSiparisFoy(ids, "GetAksesuarSiparisFoyuBySipId","AksPOSipId");
            asf.ShowPreview();
        }

        private void CreateVerticalGrid()
        {
            vgc.Rows.AddRange(new BaseRow[] {
                AddItem("stTalepId", typeof(int), RI(Rp.stTalep)),
                AddItem("TalepNo", typeof(string)),
                AddItem("TalepEdenId", typeof(int), RI(Rp.V_User)),
                AddItem("BrandId", typeof(int), RI(Rp.Brand)),
                AddItem("Numune", typeof(string)),
                AddItem("ProductCategoryId", typeof(int), RI(Rp.ProductCategory)),
                AddItem("AksesuarId", typeof(int), RI(Rp.V_Aksesuar)),
                AddItem("Tedarikci", typeof(int), RI(Rp.Vendor)),
                AddItem("Notes", typeof(string))
            });

            vgc.Rows.GetRowByFieldName("Notes", true).Height = 80;

            vgc.OptionsBehavior.UseEnterAsTab = true;
            vgc.LayoutStyle = DevExpress.XtraVerticalGrid.LayoutViewStyle.SingleRecordView;
            vgc.CustomDrawRowHeaderCell += new CustomDrawRowHeaderCellEventHandler(vgc_CustomDrawRowHeaderCell);
            vgc.Appearance.RowHeaderPanel.BackColor = Color.LightBlue;

            vgc.BestFit();
        }

        public BaseRow AddItem(string name, Type t, params RepositoryItem[] ri)
        {
            return AddItem(vgc, name, t, ri);
        }

        object GetVal(string FieldName)
        {
            return vgc.GetCellValue(vgc.Rows[FieldName], vgc.FocusedRecord);
        }

        void btnGetir_Click(object sender, EventArgs e)
        {
            Yenile();            
        }

        private void Yenile()
        {
            int focusedRow = gvAksPOSip.FocusedRowHandle;
            ds.Clear();

            gvAksPOSip.RefreshDataSource("GetAksPOatVendor",
                intParse(GetVal("stGenelId")),
                strParse(GetVal("TalepNo")),
                intParse(GetVal("TalepEdenId")),
                intParse(GetVal("BrandId")),
                strParse(GetVal("Numune")),
                intParse(GetVal("ProductCategoryId")),
                intParse(GetVal("AksesuarId")),
                intParse(GetVal("Tedarikci")),
                strParse(GetVal("Aciklama")),
                WhoAmI
                );

            gvAksPOSip.BestFitColumns();

            if (focusedRow + 1 >= gvAksPOSip.RowCount)
            {
                gvAksPOSip.FocusedRowHandle = gvAksPOSip.RowCount - 2;
            }
            else
            {
                gvAksPOSip.FocusedRowHandle = focusedRow;
            }
            Notify("Done.");
        }

        void btnTemizle_Click(object sender, EventArgs e)
        {
            foreach (BaseRow r in vgc.Rows)
                vgc.SetCellValue(r, 0, null);
        }

        private void dockPanel1_Click(object sender, EventArgs e)
        {

        }
    }
}